package com.cn.yugan.Engine.Adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.yugan.Engine.Holder.ArticleItemHolder;
import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.mobile.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Rover on 2016/7/30 .
 * Email 357319517@qq.com
 */
public class ArticleListViewAdpater extends BaseAdapter {
    private Context context;
    private ArrayList<ArticleListInfo> mlist = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ArticleItemHolder articleItemHolder;
    private String type;

    public ArticleListViewAdpater(Context context, ArrayList<ArticleListInfo> mlist, String type) {
        this.context = context;
        this.mlist = mlist;
        this.type = type;
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);


    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        articleItemHolder = null;
        if (convertView == null) {
            articleItemHolder = new ArticleItemHolder();
            convertView = layoutInflater.inflate(R.layout.list_item_normal, null);
            articleItemHolder.author = (TextView) convertView.findViewById(R.id.list_item_author);
            articleItemHolder.commit = (TextView) convertView.findViewById(R.id.list_item_commit);
            articleItemHolder.title = (TextView) convertView.findViewById(R.id.list_item_title);
            articleItemHolder.img = (ImageView) convertView.findViewById(R.id.list_item_img);
            articleItemHolder.see = (TextView) convertView.findViewById(R.id.list_item_see);
            convertView.setTag(articleItemHolder);
        } else {
            articleItemHolder = (ArticleItemHolder) convertView.getTag();
        }
        String title = mlist.get(position).getTitle();
        articleItemHolder.title.setText(Html.fromHtml(title));
        articleItemHolder.commit.setText(String.valueOf(mlist.get(position).getComment()));
        articleItemHolder.author.setText("Up主:" + mlist.get(position).getAuthor());
        articleItemHolder.see.setText("围观:" + String.valueOf(mlist.get(position).getClick()));
        LogUtils.d(position+"=====>"+mlist.get(position).getImg());
        Picasso.with(context).load(context.getString(R.string.imgserverurl)+mlist.get(position).getImg()+"!cover120.90").error(R.mipmap.default_img).into(articleItemHolder.img);
        //articleItemHolder.img;
        return convertView;
    }
    public void refreshList(ArrayList<ArticleListInfo> mlist) {
        if (mlist == null){
            
        }else {
            this.mlist = mlist;
        }
        
    }
    public void addList(ArrayList<ArticleListInfo> mlist) {
        if (mlist == null){

        }else {
            this.mlist.addAll(mlist);
        }
            
    }
}
